// 引入mongoose模块
const mongoose = require('mongoose')
// 数据库地址
const URL = 'mongodb://localhost:27017/sar'

// 连接数据库
mongoose.connect(URL,{
    useNewUrlParser: true   //兼容老版本
},err => {
    if(err){
        console.log('DB连接失败=====',err)
    }else{
        console.log('DB连接成功')
    }
})

//用户表信息
const userSchema = new mongoose.Schema({
  username:   String,
  password:   String,
  name:       String,
  sex:        {
    type:   String,
    default: 'man'
  },
  phone:      {
    type:   String,
    default: '18826402240'
  },
  email:      {
    type:   String,
    default: 'xx@qq.com'
  },
  roles:      {
    type:   Array,
    default: ['student']
  },
  // 头像
  avatar: {
    type: String,
    default: 'xxx'
  },
  // 消息列表
  messages: {
    type: Array,
    default: []
  },
  // 手藏列表
  collections: {
    type: Array,
    default: []
  },
  // 数据统计
  statistics: {
    type: Array,
    default: []
  },
  // 任务列表
  todoLists: {
    type: Array,
    default: []
  },
  // 文件列表
  files: {
    type: Array,
    default: []
  },
  // 组
  groups: {
    type: Array,
    default: []
  },
  // 回收站
  bins: {
    type: Array,
    default: []
  }
})

// 小组表
const groupSchema = new mongoose.Schema({
  groupName: String,
  imagePath: String,
  folders: Array,
  members: Array,
  setTime: String,
  creater: String,
  username: String,
  messages: Array,
  statistics: Array,
  todoLists: Array,
  bins: Array
})

//创建表并且返回一个用户表对象
module.exports.User = mongoose.model('users', userSchema);
module.exports.Group = mongoose.model('groups', groupSchema);